package com.yilin.medical.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.Gallery;

public class JxPhotoGallery extends Gallery {

	// float scale = getResources().getDisplayMetrics().density;
	// int FLINGTHRESHOLD = (int) (10.0f * scale + 0.5f);

	private IPhotoGalleryListener mListener;
	
	public void setPhotoGalleryListener(IPhotoGalleryListener listener) {
		this.mListener = listener;
	}

	public JxPhotoGallery(Context context) {
		super(context);
	}

	public JxPhotoGallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public JxPhotoGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		if(mListener != null) {
			mListener.handleAnim();
		}
		return super.onSingleTapUp(e);
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if(Math.abs(e2.getY() - e1.getY()) > 5) {
//			if (e2.getX() > e1.getX()) {
			if (e2.getX() > e1.getX() ) {
				if(mListener != null) {
					mListener.cancelLogic();
				}
				return super.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
			} else if(e2.getX() < e1.getX()){
				if(mListener != null) {
					mListener.handleLogic();
				}
				return super.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
			} else {
				return false;
			}
		} else {
			return true;
		}
	}

	public interface IPhotoGalleryListener {
		public void handleAnim();
		public void handleLogic();
		public void cancelLogic();
	}
}
